#include<stdio.h>
#include<math.h>
double qiu_d2(int y0,int xr,int xa)
{
	return (((double)(y0*xr)-(double)(xa*y0))*((double)(y0*xr)-(double)(xa*y0)))/((double)(xa*xa)+(double)(y0*y0));
}
double qiu_a(double d2,int xr,int xa,int r)
{
	return sqrt((xr-xa)*(xr-xa)-d2)-sqrt(r*r-d2);
}
double qiu_b(double d2,int xr,int xa,int r)
{
	return 2*sqrt(r*r-d2);
}
int main(int argc,char *argv[])
{
	int t;
	scanf("%d",&t);
	while(t--)
	{

		double reduce=0;
		double d2;
		long long sum=0;
		double temp;
		int xa,ya,xr,r,n,y0;
		int i;
		double a;
		double b;
		scanf("%d %d %d %d %d",&xa,&ya,&xr,&r,&n);
		long long c[n+1];
		for(i=1; i<=n; i++)
		{
			temp=0;
			scanf("%d %lld",&y0,&c[i]);
			y0=y0-ya;
			d2=qiu_d2(y0,xr,xa);
			a=qiu_a(d2,xr,xa,r);
			b=qiu_b(d2,xr,xa,r);
//			printf("%f\n",d2);
//			printf("%f %f\n",a,b);
//			printf("c[i]-a*a-a*b=%f\n",c[i]-a*a-a*b);
			if(d2<r*r)
			{
				if((c[i]-a*a-a*b)>0)
				{
					temp=a*a+a*b;
				}
				else
				{
					temp=c[i];
				}
			}
//			printf("temp=%f\n",temp);
			reduce+=temp;
			sum+=c[i];
		}
//		printf("%f\n",sum);
		printf("%.f\n",sum-reduce);
	}
	return 0;
}
